package com.yixin.hospital.controller;

import com.yixin.hospital.common.ApiResult;
import com.yixin.hospital.common.Constant;
import com.yixin.hospital.entity.dto.req.jobtitle.JobTitleAddReqDTO;
import com.yixin.hospital.entity.dto.req.jobtitle.JobTitleEditReqDTO;
import com.yixin.hospital.service.JobTitleService;
import com.yixin.hospital.utils.JwtUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

@RestController
@RequestMapping("/job-title")
@Api(value = "职称管理", tags = "职称管理")
public class JobTitleController {

    @Resource
    private  JobTitleService jobTitleService;

    @GetMapping("/list")
    @ApiOperation(value = "职称列表")
    public ApiResult list() {
        return jobTitleService.allList();
    }

    @GetMapping("/list-tree")
    @ApiOperation(value = "职称列表(树状)")
    public ApiResult listTree() {
        return jobTitleService.treeList();
    }

    @GetMapping("/job-job-title")
    @ApiOperation(value = "根据职业查询职称")
    @ApiImplicitParams({
            @ApiImplicitParam(name="jobId",value="职业id", paramType = "query")
    })
    public ApiResult deptWorkAddress(@RequestParam Long jobId) {
        return jobTitleService.jobJobTitle(jobId);
    }

    @PostMapping("/add")
    @ApiOperation(value = "添加职称")
    public ApiResult add(@Valid @RequestBody JobTitleAddReqDTO jobTitleAddReqDTO, HttpServletRequest request) {
        String userName = JwtUtil.getName(request.getHeader(Constant.AUTHORIZATION));
        return jobTitleService.add(jobTitleAddReqDTO, userName);
    }

    @PutMapping("/edit")
    @ApiOperation(value = "编辑职称")
    public ApiResult edit(@Valid @RequestBody JobTitleEditReqDTO jobTitleEditReqDTO, HttpServletRequest request) {
        String userName = JwtUtil.getName(request.getHeader(Constant.AUTHORIZATION));
        return jobTitleService.edit(jobTitleEditReqDTO, userName);
    }

    @DeleteMapping("/remove/{id}")
    @ApiOperation(value = "删除职称")
    public ApiResult remove(@PathVariable int id) {
        jobTitleService.removeById(id);
        return ApiResult.success();
    }
}
